其他
Go:完了,我成C++了
文 | 局长
出品 | OSC开源社区(ID:oschina2013)
arena
包,arena
包可用于分配任意数量的 arena,可以从 arena 的内存中分配任意类型的对象,并且 arena 会根据需要自动增长大小。当一个 arena 中的所有对象不再使用时,可以显式释放该 arena 以有效地回收其内存,而无需进行常见的垃圾回收操作。我们要求此实现提供安全检查,如果 arena 操作不安全,程序将在任何不正确的行为发生之前终止。为了获得最大的灵活性,API 能够分配任何类型的对象和切片,包括可以在运行时通过反射生成的类型。type Arena struct {
// contains filtered or unexported fields
}
// New allocates a new arena.
func New() *Arena
// Free frees the arena (and all objects allocated from the arena) so that
// memory backing the arena can be reused fairly quickly without garbage
// collection overhead. Applications must not call any method on this
// arena after it has been freed.
func (a *Arena) Free()
// New allocates an object from arena a. If the concrete type of objPtr is
// a pointer to a pointer to type T (**T), New allocates an object of type
// T and stores a pointer to the object in *objPtr. The object must not
// be accessed after arena a is freed.
func (a *Arena) New(objPtr interface{})
// NewSlice allocates a slice from arena a. If the concrete type of slicePtr
// is *[]T, NewSlice creates a slice of element type T with the specified
// capacity whose backing store is from the arena a and stores it in
// *slicePtr. The length of the slice is set to the capacity. The slice must
// not be accessed after arena a is freed.
func (a *Arena) NewSlice(slicePtr interface{}, cap int)
“arena”
…
)
type T struct {
val int
}
func main() {
a := arena.New()
var ptrT *T
a.New(&ptrT)
ptrT.val = 1
var sliceT []T
a.NewSlice(&sliceT, 100)
sliceT[99] .val = 4
a.Free()
}
对于Go语言的新提案,引起了网友们热烈讨论:
对于 Go 语言的新提案,你有什么想说的吗?评论区等你~
往期精彩回顾